import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rive/rive.dart';

class FlareDemo extends StatefulWidget {
  @override
  _FlareDemoState createState() => _FlareDemoState();
}

class _FlareDemoState extends State<FlareDemo>
    with SingleTickerProviderStateMixin {
  RiveFile file;
  bool playAnimatSate = false;

  Artboard _riveArtboard;
  RiveAnimationController controller;

  void _togglePlay() {
    setState(() {
      final artboard = file.mainArtboard;
      artboard.addController(controller =
          SimpleAnimation(playAnimatSate ? 'night_day' : 'day_night'));
      playAnimatSate = !playAnimatSate;
    });
  }

  @override
  void initState() {
    super.initState();

    rootBundle.load('assets/knight063.riv').then(
      (data) async {
        file = RiveFile();

        if (file.import(data)) {
          final artboard = file.mainArtboard;
          artboard.addController(controller = SimpleAnimation('idle'));
          setState(() => _riveArtboard = artboard);
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: _riveArtboard == null
            ? const SizedBox()
            : Rive(artboard: _riveArtboard),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _togglePlay,
        child: Icon(
          playAnimatSate ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }
}
